﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Odbc;
using System.Data;

namespace LectorHuellaC
{
    class cls_category
    {
        log4net.ILog logger;
        ConectaBD conBD = new ConectaBD();

        OdbcCommand cm;
        OdbcParameter prm;
        OdbcDataReader dr;

        public cls_category()
        {
            log4net.Config.XmlConfigurator.Configure();
            logger = log4net.LogManager.GetLogger(typeof(Program));
        }

        public DataTable getCategories()
        {
            string product_info = "call getCategories()";
            conBD.ConectaODBC();
            cm = new OdbcCommand();
            cm.Connection = conBD.ConexionODBC;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = product_info;
            DataTable table = new DataTable();
            OdbcDataAdapter da = new OdbcDataAdapter(cm);
            da.Fill(table);
            return table;
        }

        public DataTable getCategoriesbyCriteria(string search)
        {
            try
            {
                string product_info = "call getCategoriesByCriteria(?)";
                conBD.ConectaODBC();
                cm = new OdbcCommand();
                cm.Connection = conBD.ConexionODBC;
                cm.CommandType = CommandType.StoredProcedure;
                cm.CommandText = product_info;

                cm.Parameters.AddWithValue("search", search);
                //cm.Parameters.AddWithValue("category_id", product_id);
                //OdbcDataReader reader = cm.ExecuteReader();
                //DataSet ds = new DataSet();
                DataTable table = new DataTable();
                OdbcDataAdapter da = new OdbcDataAdapter(cm);
                da.Fill(table);
                return table;
            }
            catch (Exception ex)
            {
                logger.Error("" + ex.Message);
                return null;
            }
        }

        public bool insertarActualizarCategory(int category_id, string namecategory, string description)
        {
            string insertar_actualizar = "";
            if (category_id == 0)
            {
                insertar_actualizar = "call InsertarCategoriaProducto (?,?)";

            }
            else
            {
                insertar_actualizar = "call actualizarCategoriaProducto (?,?,?) ";
            }

            //Use ODBC call syntax.
            conBD.ConectaODBC();
            cm = new OdbcCommand();
            cm.Connection = conBD.ConexionODBC;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = insertar_actualizar;
            if (category_id != 0)
            {
                cm.Parameters.AddWithValue("_category_id", category_id);
            }
            cm.Parameters.AddWithValue("_category_name", namecategory);
            cm.Parameters.AddWithValue("_category_description", description);
            int rowsaffected = cm.ExecuteNonQuery();

            if (rowsaffected == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


    }
}
